// DlgSettingUser.cpp : implementation file
//

#include "stdafx.h"
#include "XIE.h"
#include "DlgSettingUser.h"


// CDlgSettingUser dialog

IMPLEMENT_DYNCREATE(CDlgSettingUser, CDHtmlDialog)

CDlgSettingUser::CDlgSettingUser(CWnd* pParent /*=NULL*/)
	: CDHtmlDialog(CDlgSettingUser::IDD, NULL, pParent)
{
	m_bAutoClose = FALSE;
}

CDlgSettingUser::~CDlgSettingUser()
{
}

void CDlgSettingUser::DoDataExchange(CDataExchange* pDX)
{
	CDHtmlDialog::DoDataExchange(pDX);
}

BOOL CDlgSettingUser::OnInitDialog()
{
	CDHtmlDialog::OnInitDialog();
	CString strURL;		

	DWORD dwOEMID = AfxGetApp()->GetProfileInt(_T("OEM"), _T("ID"), 0);
	strURL.Format(IDS_USER_URL, dwOEMID, (LPCTSTR)theApp.m_strUserName);
	//CURLEncode url;
	//CString strURL1 = url.URLEncode((LPCTSTR)strURL);
	if (!strURL.IsEmpty())
	{
		Navigate(strURL);
		//Navigate(_T("file:///d:/index.html"));
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

BEGIN_MESSAGE_MAP(CDlgSettingUser, CDHtmlDialog)
END_MESSAGE_MAP()

BEGIN_DHTML_EVENT_MAP(CDlgSettingUser)
	DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK)
	DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel)
END_DHTML_EVENT_MAP()



// CDlgSettingUser message handlers

HRESULT CDlgSettingUser::OnButtonOK(IHTMLElement* /*pElement*/)
{
	//OnOK();
	return S_OK;
}

HRESULT CDlgSettingUser::OnButtonCancel(IHTMLElement* /*pElement*/)
{
	//OnCancel();
	return S_OK;
}

void CDlgSettingUser::OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
	CDHtmlDialog::OnDocumentComplete(pDisp, szUrl);

	
	CString strDocURL;
	GetCurrentUrl(strDocURL);
	if (strDocURL.IsEmpty() || strDocURL != szUrl)
		return;

	IHTMLDocument2* pDocument = NULL;	
	
	CComBSTR bstrValue; 

	HRESULT hr = GetDHtmlDocument(&pDocument);

	if(FAILED(hr) || !pDocument)
		return;

	hr  = pDocument->get_cookie(&bstrValue);
	if(FAILED(hr))
		return;

	CString str = bstrValue;	

	if (str.IsEmpty())
		return;

	const TCHAR* cpszPrefixUID = _T("UID=");
	const TCHAR* cpszPrefixUName = _T("alipay_account=");
	BOOL bFoundUID = FALSE;

	int nFoundIndex = str.Find(cpszPrefixUID);
	if (nFoundIndex >= 0)
	{
		CString strUID = str.Mid(_tcslen(cpszPrefixUID) + nFoundIndex, str.GetLength() - _tcslen(cpszPrefixUID));
		int nFoundIndex = strUID.Find(_T(";"));
		if (nFoundIndex > 0)
			strUID = strUID.Left(nFoundIndex);

		if (!strUID.IsEmpty())
		{
			theApp.m_strUserID = strUID;		

			bFoundUID = TRUE;						
		}
	}	

	nFoundIndex = str.Find(cpszPrefixUName);
	if (nFoundIndex >= 0)
	{
		CString strUName = str.Mid(_tcslen(cpszPrefixUName) + nFoundIndex, str.GetLength() - _tcslen(cpszPrefixUName));
		int nFoundIndex = strUName.Find(_T(";"));
		if (nFoundIndex > 0)
			strUName = strUName.Left(nFoundIndex);

		CURLEncode url;

		strUName = url.URLDecode(strUName);

		if (!strUName.IsEmpty())
		{
			theApp.m_strUserName = strUName;												
		}
	}	

	if (bFoundUID)
	{
		AfxGetApp()->WriteProfileString(_T("user"), _T("id"), theApp.m_strUserID);
		AfxGetApp()->WriteProfileString(_T("user"), _T("name"), theApp.m_strUserName);
		if (m_bAutoClose)
			CDialog::OnOK();
	}

	// TODO: Add your specialized code here and/or call the base class
}
